//  this library.
import 'package:flutter/cupertino.dart';
import 'package:hktk_app/model/course/course_chapter_model.dart';
import 'package:hktk_app/model/course/course_handouts_model.dart';
import 'package:hktk_app/model/course/note_video_model.dart';
import 'package:hktk_app/model/course/video_info_model.dart';
import 'package:hktk_app/model/course/video_save_model.dart';
import 'package:hktk_app/model/page/resource_page_model.dart';
import 'package:hktk_app/util/dao_util.dart';

/// 课程、视频相关接口
class CourseDao {
  /// 通过栏目获取章节（视频）
  static Future<List<CourseChapterModel>> listByColumn({
    required String industryCode,
    required String examId,
    required int categoryId,
    required String columnId,
  }) async {
    String url =
        "/kc/course/video/chapter/list-by-column?industryCode=$industryCode&examId=$examId&categoryId=$categoryId&columnId=$columnId";
    // 发送请求
    var data = await httpGET(url: url, tips: "通过栏目获取章节（视频）");
    if (data is List<dynamic>) {
      return data.map((value) => CourseChapterModel.fromJson(value)).toList();
    }
    return <CourseChapterModel>[];
  }

  // pId：父级章节id
  /// 获取子章节（视频）
  static Future<List<CourseChapterModel>> childChapterVideo({
    required String industryCode,
    required String examId,
    required int categoryId,
    required String columnId,
    required String pId,
  }) async {
    String url =
        "/kc/course/video/chapter/child-chapter?industryCode=$industryCode&examId=$examId&categoryId=$categoryId&columnId=$columnId&pid=$pId";
    // 发送请求
    var data = await httpGET(url: url, tips: "获取子章节（视频）");
    if (data is List<dynamic>) {
      return data.map((value) => CourseChapterModel.fromJson(value)).toList();
    }
    return <CourseChapterModel>[];
  }

  /// 根据章节id获取视频
  static Future<List<VideoInfoModel>> listByChapter({
    required String industryCode,
    required String examId,
    required int categoryId,
    required String columnId,
    required String chapterId,
  }) async {
    String url =
        "/kc/course/video/list-by-chapter?industryCode=$industryCode&examId=$examId&categoryId=$categoryId&columnId=$columnId&ChapterId=$chapterId";
    // 发送请求
    var data = await httpGET(url: url, tips: "根据章节id获取视频");
    if (data is List<dynamic>) {
      return data.map((value) => VideoInfoModel.fromJson(value)).toList();
    }
    return <VideoInfoModel>[];
  }

  /// 获取播放凭证
  static Future<String> aliyunAuth({
    required String columnId,
    required String topChapterId,
    required String videoId,
  }) async {
    Map<String, dynamic> params = {
      "columnId": columnId,
      "topChapterId": topChapterId,
      "videoId": videoId,
    };
    String url = "/kc/course/video/aliyun-auth";
    // 发送请求
    var data = await httpPOST(url: url, params: params, tips: "获取播放凭证");
    if (data is String) {
      return data;
    }
    return "";
  }
  static Future<String> aliyunAuthFree({
    required String videoId,
  }) async {
    Map<String, dynamic> params = {
      "videoId": videoId,
    };
    String url = "/kc/course/video/aliyun-auth-free";
    // 发送请求
    var data = await httpPOST(url: url, params: params, tips: "获取播放凭证");
    if (data is String) {
      return data;
    }
    return "";
  }

  /// 获取水印方法
  static Future<String> watermark() async {
    String url = "/user/sys/group/watermark";
    // 发送请求
    var data = await httpGET(url: url, tips: "获取水印");
    return data;
  }

  /// 保存视频播放记录
  static Future<void> saveVideoRecords({
    required String industryCode,
    required String examId,
    required int categoryId,
    required String columnId,
    required String chapterId,
    required Map<String, VideoSaveModel> data,
  }) async {
    Map<String, dynamic> params = {
      "industryCode": industryCode,
      "examId": examId,
      "categoryId": categoryId,
      "columnId": columnId,
      "ChapterId": chapterId,
      "data": data,
    };
    String url = "/kc/course/video/save-video-records";
    // 发送请求
    var res=await httpPOST(
      url: url,
      params: params,
      tips: "保存视频播放记录",
    );

  }

  /// 根据视频id获取视频资料
  static Future<List<CourseHandoutsModel>> listByVideo({
    required String videoId,
  }) async {
    String url = "/kc/course/video/handout/list-by-video?videoId=$videoId";
    // 发送请求
    var data = await httpGET(
      url: url,
      tips: "根据视频id获取视频资料",
    );
    if (data is List<dynamic>) {
      return data.map((value) => CourseHandoutsModel.fromJson(value)).toList();
    }
    return <CourseHandoutsModel>[];
  }

  /// 添加下载次数方法（课程）
  static Future<void> addFrequency({
    required String resId,
  }) async {
    Map<String, dynamic> params = {
      "id": resId,
    };
    String url = "/kc/course/video/handout/add-frequency";
    // 发送请求
    await httpPut(
      url: url,
      params: params,
      tips: "添加下载次数方法（课程）",
    );
  }

  /// 分页获取笔记集合
  static Future<ResourcePageModel<NoteVideoModel>> notePageList({
    required int current,
    required int size,
  }) async {
    String url = "/kc/course/video/note/page?current=$current&size=$size";
    // 发送请求
    var data = await httpGET(
      url: url,
      tips: "分页获取笔记集合",
    );
    if (data != null) {
      return ResourcePageModel<NoteVideoModel>.fromJson(data);
    }
    return ResourcePageModel<NoteVideoModel>();
  }

  /// 发表笔记（视频）
  static Future<dynamic> noteUpdate({
    required NoteVideoModel noteVideoModel,
  }) async {
    String url = "/kc/course/video/note/update";
    // 发送请求
    var data = await httpPOST(
      url: url,
      params: noteVideoModel.toJson(),
      tips: "发表笔记（视频）",
    );
    if (data != null) {
      return NoteVideoModel.formJson(data);
    }
    return NoteVideoModel();
  }

  static Future<dynamic> getPlayInfo({
    required String videoId,
  }) async {
    String url = "/kc/course/video/GetPlayInfo";
    // 发送请求
    var data = await httpGET1(
      url: url,
      params: {
        "videoId": videoId,
      },
      tips: "",
    );

    return data;
  }
  static Future<dynamic> teacherList({
    required int current,
    required int size,
  }) async {
    String url = "/user/sys/teacher/list";
    // 发送请求
    var data = await httpGET1(
      url: url,
      params: {
        "current": current.toString(),
        "size": size.toString(),
      },
      tips: "",
    );

    return data;
  }
  static Future<dynamic> getVideoRecords({
    required String industryCode,
    required String examId,
    required int current,
    required int size,
  }) async {
  //
    String url = "/kc/course/video/getVideoRecords";
    return await httpGET1(
      url: url,
      params: {
        "current": current.toString(),
        "size": size.toString(),
        "industryCode": industryCode,
        "examId": examId,
      },
      tips: "",
    );
  }

}
